﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluggableModulesInterface;
using SMSROUTER.Business;
using System.Threading.Tasks;
using Router1.SmsWebService;
using StorageManagement;
using Settings;
using System.Data.Linq;
using PluggableModulesRouter;
using SMSROUTER.StorageManagement;

namespace Router1
{
    public class Router1Manage : WorkingBaseMTWebServiceRouter
    {
        #region Declaretion
        private DateTime currentSent = DateTime.MinValue;
        private SMSMTService _SmsRouterObject = new SMSMTService();
        protected override System.Web.Services.Protocols.SoapHttpClientProtocol SmsRouterObject
        {
            get
            {
                return _SmsRouterObject;
            }
        }
        #endregion
        protected override void SendSms(List<SmsOutWaiting> smsOut)
        {
            int threadID = ThreadID;
            bool enableGetReport = ENABLE_GETREPORT;
            List<SmsRouterReport> listRouterReport = new List<SmsRouterReport>();
            foreach (var item in smsOut)
            {
                try
                {
                    string result = "";
                    result = (SmsRouterObject as SMSMTService).SendMT(null, null, item.Message, item.ShortCode, item.DestAddr, false, false);
                    if (enableGetReport)
                    {
                        listRouterReport.Add(new SmsRouterReport(item.RouterID, item.SmsOutGUID, item.CooperateGUID, DateTime.Now, null, AppSetting.Status.SENTGATEWAY, threadID, result, null));
                    }
                    SmsOutReportStorage.EnqueueElementData(Key, SmsOutReportStruct.CreateList(smsOut, DateTime.Now, DateTime.Now, AppSetting.Status.SENT, threadID));
                    //CanculateSpeedSent(smsOut.Count);
                }
                catch (Exception ex)
                {
                    HandleException<SmsOutWaiting>(ex, Name, item);
                }
            }

            //Insert dữ liệu report
            if (listRouterReport.Count > 0)
            {
                SmsRouterReport.BulkInsert(listRouterReport);
            }
        }

        private void CanculateSpeedSent(int countItem)
        {
            Console.WriteLine("Toc do gui tin: {0}/giay", countItem / (DateTime.Now - currentSent).TotalSeconds);
            currentSent = DateTime.Now;
        }

        protected override void GetReport()
        {
            long startID = 1;
            List<SmsRouterReport> listRouterReport = new List<SmsRouterReport>();
            do
            {
                listRouterReport = SmsRouterReport.GetByRouterID(this.Key, startID, this.BatchSize);
                if (listRouterReport.Count > 0)
                {
                    startID = listRouterReport[listRouterReport.Count - 1].ReportID + 1;
                    foreach (SmsRouterReport routerReport in listRouterReport)
                    {
                        string result = (SmsRouterObject as SMSMTService).GetReport(int.Parse(routerReport.StringParam));
                        switch (result)
                        {
                            case "SUCCESSFUL":
                                routerReport.Status = AppSetting.Status.SENT;
                                routerReport.ReportTime = DateTime.Now;
                                break;
                            case "FAIL":
                                routerReport.Status = AppSetting.Status.FAIL;
                                routerReport.ReportTime = DateTime.Now;
                                break;
                        }
                    }
                    SmsRouterReport.BatchUpdate(listRouterReport);
                }
            }
            while (listRouterReport.Count > 0);
        }
    }
}
